﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TruongVu.Business;

namespace TruongVuWebsite.Admin
{
    public partial class DecreeManagement : System.Web.UI.Page
    {
        private DocumentModel _documentModel = new DocumentModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DecreeGridView.DataSource = _documentModel.GetAll();
            this.DecreeGridView.DataBind();
        }

        protected void DecreeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<TruongVu.Data.Document> documents = this.DecreeGridView.DataSource as List<TruongVu.Data.Document>;
            if (documents != null && documents.Count > e.RowIndex)
            {
                this._documentModel.Delete(documents[e.RowIndex].ID);

                // Delete File
                string fileName =Server.MapPath(documents[e.RowIndex].Content);
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }

                documents.RemoveAt(e.RowIndex);
                this.DecreeGridView.DataBind();
            }
        }

        protected void DecreeGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            List<TruongVu.Data.Document> documents = this.DecreeGridView.DataSource as List<TruongVu.Data.Document>;
            if (documents != null && documents.Count > e.NewEditIndex)
            {
                Response.Redirect("Decree.aspx?ID=" + documents[e.NewEditIndex].ID);
            }
        }
    }
}